package com.studyinghome.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyinghome.entity.Order;
import com.studyinghome.entity.OrderItem;
import com.studyinghome.mapper.OrderMapper;
import com.studyinghome.service.OrderItemService;
import com.studyinghome.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Leslie
 * @create 2023-02-10 14:46
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderItemService orderItemService;

    @Override
    public Order queryById(Long id) {
        Order order = this.getById(id);
        if (ObjectUtil.isNull(order)) {
            return null;
        }
        List<OrderItem> items = orderItemService.listByOrderNo(order.getOrderNo());
        order.setOrderItems(items);
        return order;
    }

    @Override
    public Order queryByOrderNo(String orderNo) {
        Order order = this.lambdaQuery().eq(Order::getOrderNo, orderNo).one();
        if (ObjectUtil.isNull(order)) {
            return null;
        }
        List<OrderItem> items = orderItemService.listByOrderNo(order.getOrderNo());
        order.setOrderItems(items);
        return order;
    }

    @Override
    public OrderItem queryItem(Long itemId) {
        return orderItemService.getById(itemId);
    }
}
